<?php

declare (strict_types=1);
namespace App\Model;

use App\Constants\ErrorCode;
use Hyperf\Di\Annotation\Inject;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;

class CloudBase extends Model
{
    /**
     * @Inject
     * @var RequestInterface
     */
    protected $request;

//    省市区查询字段
    const  AREA_FIELD = ['id','pid','name','shortname'];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'open_time' => 'array',
    ];
/**
*小程序端默认为关联app_user
 **/
    const APP_USER_FIELD = ['id','username','nickname','phone','compellation','avatar'];
    public function create_info()
    {
        return $this->belongsTo(AppUser::class,'create_by','id')->select(self::APP_USER_FIELD);
    }
    public function update_info()
    {
        return $this->belongsTo(AppUser::class,'update_by','id')->select(self::APP_USER_FIELD);
    }
    public function delete_info()
    {
        return $this->belongsTo(AppUser::class,'delete_by','id')->select(self::APP_USER_FIELD);
    }
    public function file_info()
    {
        return $this->belongsTo(Fileinfo::class,'fileinfo_id','fileinfo_id')->select(['fileinfo_id','url','imagetype','mimetype']);
    }

    public function province()
    {
        return $this->belongsTo(Areainfo::class,'province_id','id')->select(self::AREA_FIELD);
    }
    public function city()
    {
        return $this->belongsTo(Areainfo::class,'city_id','id')->select(self::AREA_FIELD);
    }
    public function area()
    {
        return $this->belongsTo(Areainfo::class,'area_id','id')->select(self::AREA_FIELD);
    }
    public function street()
    {
        return $this->belongsTo(Areainfo::class,'street_id','id')->select(self::AREA_FIELD);
    }

    public function shop_info()
    {
        $fields = ['shop_name','id','type','business_scope','business_scope','shop_desc','shop_type','open_time','shop_state','link_name','phone','join_dc_id','lng','lat','address_id'];
        return $this->belongsTo(FlyShop::class, 'shop_id' ,'id')
            ->with('apply_info')
            ->with('with_industry')
            ->with('certificate_att')
            ->with('center_info')
            ->select($fields);
    }
    public function food_shop_info()
    {
        $lat =  $this->request->input('lat',0);
        $lng =  $this->request->input('lng',0);
        if($lat&&$lng){
            $fields = ['shop_name','id','type','business_scope','business_scope','shop_desc','shop_type','open_time','shop_state','link_name','phone','food_state','join_dc_id','lng','lat','address_id',
                Db::raw("st_distance_sphere(point(120.73756,30.74014),point(lng,lat)) as distance") ];
        }else{
            $fields = ['shop_name','id','type','business_scope','business_scope','shop_desc','shop_type','open_time','shop_state','link_name','phone','join_dc_id','lng','lat','address_id'];
        }
        return $this->belongsTo(FlyShop::class, 'shop_id' ,'id')
            ->with('apply_info')
            ->with('with_industry')
            ->with('center_info')
            ->where("food_state",1)
            ->where("food_switch",1)
            ->select($fields);
    }

    public function landmark_info()
    {
        $fields = ['id','name','province_id','city_id','area_id','street_id'];
        return $this->belongsTo(Landmark::class,'landmark_id','id')
            ->with(['province','city','area','street'])
            ->select($fields);
    }

    public function industry_info()
    {
        $fields = ['*'];
        return $this->belongsTo(Industry::class, 'industry_id' ,'id')
            ->with(['with_virtual_params'])
            ->select($fields);
    }
    public function delivery_info()
    {
        $fields = ['*'];
        return $this->belongsTo(DeliveryTpl::class, 'delivery_id' ,'id')
            ->select($fields);
    }

    public function user_info()
    {
        return $this->belongsTo(AppUser::class,'user_id','id')->select(self::APP_USER_FIELD);
    }

    //    商品详情
    public function product_info()
    {
        $fields = ['id','product_name','min_single_price'];
        return $this->belongsTo(Product::class, 'product_id' ,'id')
            ->with('main_pic')
            ->select($fields);
    }
}